package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.commons.utils.BeanCopyUtil;
import com.woniuxy.dao.model.MenuInfo;
import com.woniuxy.dao.MenuInfoDao;
import com.woniuxy.dao.model.RoleMenu;
import com.woniuxy.service.MenuInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.service.RoleMenuService;
import com.woniuxy.service.dto.MenuAccountDto;
import com.woniuxy.service.dto.MenuDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author psy
 * @since 2022-06-28
 */
@Service
public class MenuInfoServiceImpl extends ServiceImpl<MenuInfoDao, MenuInfo> implements MenuInfoService {

    @Resource
    private MenuInfoDao menuInfoDao;

    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public List<MenuDto> loadMenuByAccount(Integer accountId) {
        //一次性查出账号id所对应的角色的所有菜单
        List<MenuInfo> menuInfos = menuInfoDao.selectMenuByAccount(accountId);
        //组装成树形结构
        ArrayList<MenuDto> menuDtos = new ArrayList<>(); //存放根节点的集合
        HashMap<Integer, MenuDto> maps = new HashMap<>();

        if(menuInfos != null && menuInfos.size() >0){
            for(MenuInfo menuInfo : menuInfos){
                MenuDto menuDto = BeanCopyUtil.copyOne(menuInfo, MenuDto.class);
                if(menuInfo.getMenuParentId() == 0){
                    menuDtos.add(menuDto);
                }
                maps.put(menuInfo.getMenuId(),menuDto);
            }
        }

        for(MenuDto md : maps.values()){
            MenuDto parent = maps.get(md.getMenuParentId());
            if(parent != null){
                parent.getChildren().add(md);
            }
        }

        return menuDtos;
    }

    /**
     * 超级管理员给商家授权菜单时加载所有菜单的接口
     * @param roleId
     * @return
     */
    @Override
    public MenuAccountDto loadMenus(Integer roleId) {

        //一次性查找所有菜单数据
        List<MenuInfo> menus = menuInfoDao.selectList(null);
        //组装成前端需要的树形结构
        List<MenuDto> menuDtos = new ArrayList<MenuDto>();   //存放根节点的集合
        Map<Integer,MenuDto> maps = new HashMap<>();
        if(menus != null && menus.size() > 0){
            for(MenuInfo m : menus){
                MenuDto md = BeanCopyUtil.copyOne(m, MenuDto.class);
                if(m.getMenuParentId() == 0){
                    menuDtos.add(md);
                }
                maps.put(md.getMenuId(),md);
            }

            for(MenuDto md : maps.values()){
                MenuDto parent = maps.get(md.getMenuParentId());
                if(parent != null){
                    parent.getChildren().add(md);
                }
            }
        }
        List<Integer> ids = new ArrayList<>();
        List<RoleMenu> mis = roleMenuService.list(new QueryWrapper<RoleMenu>().eq("roleId",roleId));

        if(mis != null && mis.size()>0){
            ids = mis.stream().map(mi -> mi.getMenuId()).collect(Collectors.toList());

            List<MenuInfo> ms = this.listByIds(ids);

            ids = ms.stream().filter(mi -> mi.getMenuState() == 1).map(mi -> mi.getMenuId()).collect(Collectors.toList());
        }

        MenuAccountDto mad = new MenuAccountDto();

        mad.setIds(ids);

        mad.setMenus(menuDtos);

        return mad;
    }




}
